{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "from lxml import etree\n",
    "html_str = \"\"\"\n",
    "<body>\n",
    "<div class=\"ui container\">\n",
    "\n",
    "        <table class=\"ui striped  table\">\n",
    "            <tr class=\"one\">\n",
    "                <th>姓名</th>\n",
    "                <th>性别</th>\n",
    "                <th>邮箱</th>\n",
    "                <th>电话</th>\n",
    "            </tr>\n",
    "            <tr class=\"one two\">\n",
    "                <td><a href=\"zhangwei\">张伟</a></td>\n",
    "                <td>男</td>\n",
    "                <td>zhangwei@haoren.com</td>\n",
    "                <td>12138-111</td>\n",
    "            </tr>\n",
    "            <tr class=\"two\">\n",
    "                <td><a href=\"yifei\">一菲</a></td>\n",
    "                <td>女</td>\n",
    "                <td>yifei@haoren.com</td>\n",
    "                <td>12138-112</td>\n",
    "            </tr>\n",
    "            <tr class=\"one three\">\n",
    "                <td><a href=\"xiaoxian\">小贤</a></td>\n",
    "                <td>男</td>\n",
    "                <td>xiaoxian@haoren.com</td>\n",
    "                <td>12138-113</td>\n",
    "            </tr>\n",
    "            <tr class=\"one three four\">\n",
    "                <td><a href=\"meijia\">美嘉</a></td>\n",
    "                <td>女</td>\n",
    "                <td>meijia@haoren.com</td>\n",
    "                <td>12138-114</td>\n",
    "            </tr>\n",
    "            <tr class=\"five\">\n",
    "                <td><a href=\"xiaobu\">小布</a></td>\n",
    "                <td>男</td>\n",
    "                <td>xiaobu@hundan.com</td>\n",
    "                <td>12138-115</td>\n",
    "            </tr>\n",
    "\n",
    "        </table>\n",
    "</div>\n",
    "</body>\n",
    "\"\"\"\n",
    "\n",
    "html = etree.HTML(html_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Element tr at 0x7fafffd6e460>,\n",
       " <Element tr at 0x7fafffd65910>,\n",
       " <Element tr at 0x7fafffd65730>,\n",
       " <Element tr at 0x7fafffd656e0>,\n",
       " <Element tr at 0x7fafffd655f0>,\n",
       " <Element tr at 0x7fafffd655a0>]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//table/tr')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['one', 'one two', 'one three', 'one three four']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath(\".//table/tr[contains(@class, 'one')]/@class\") # contains包含检索"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['one', 'one two']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//table/tr[contains(@class, \"one\") and not(contains(@class, \"three\"))]/@class') # contains + and + not"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['one']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//table/tr[1]/@class')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张伟', '男', 'zhangwei@haoren.com', '12138-111']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "html.xpath('.//table/tr[2]/td//text()')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "# 当索引不存在时，返回空列表，不报错\n",
    "print(html.xpath('.//table/tr[0]/td//text()'))\n",
    "print(html.xpath('.//table/tr[9]/td//text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['zhangwei@haoren.com']\n",
      "['zhangwei@haoren.com']\n",
      "['zhangwei@haoren.com']\n"
     ]
    }
   ],
   "source": [
    "# 两种写法相同\n",
    "print(html.xpath('.//table/tr[2]/td[3][contains(text(), \"haoren\")]//text()'))\n",
    "print(html.xpath('.//table/tr[2]/td[3 and contains(text(), \"haoren\")]//text()'))\n",
    "print(html.xpath('.//table/tr[2]/td[3]//text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 推荐另一种方式：position()定位"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['one', 'one two', 'two', 'one three', 'one three four', 'five']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table/tr/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['one two', 'two', 'one three', 'one three four', 'five']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table/tr[position()>1]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['one three', 'one three four']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table/tr[position()>1 and contains(@class, \"three\")]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['one two', 'two', 'five']\n"
     ]
    }
   ],
   "source": [
    "print(html.xpath('.//table/tr[position()>1 and not(contains(@class, \"three\"))]/@class'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['张伟', '男', 'zhangwei@haoren.com', '12138-111', '一菲', '女', 'yifei@haoren.com', '12138-112', '小贤', '男', 'xiaoxian@haoren.com', '12138-113', '美嘉', '女', 'meijia@haoren.com', '12138-114', '小布', '男', 'xiaobu@hundan.com', '12138-115']\n",
      "['12138-111', '12138-112', '12138-113', '12138-114', '12138-115']\n"
     ]
    }
   ],
   "source": [
    "## 取末尾的位置\n",
    "print(html.xpath('.//table/tr/td//text()'))\n",
    "print(html.xpath('.//table/tr/td[last()]//text()'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
